<template>
  <div class="p-2">
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <!-- 隐藏的按钮 -->
        <a-button v-show="false" type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button v-show="false" type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button v-show="false" type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-show="false" v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>
            批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
        <!-- 高级查询 -->
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
        <a-button type="primary" danger @click="handleSyncOnlineTemplate" preIcon="ant-design:sync-outlined"> 同步线上模板</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <!-- 隐藏操作按钮 -->
        <TableAction v-show="false" :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <EsignContractsModal ref="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="esign_contracts-esignContracts" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, superQuerySchema, searchFormSchema } from './EsignContracts.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, syncOnlineTemplates, clearAllData } from './EsignContracts.api';
  import EsignContractsModal from './components/EsignContractsModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  const queryParam = reactive<any>({});
  const registerModal = ref();
  const { createMessage } = useMessage();
  //注册 table 数据
  const { tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '电子签合同模板',
      api: list,
      columns,
      canResize: false,
      useSearchForm: true,
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
      },
      actionColumn: {
        width: 0,
        fixed: 'right',
        ifShow: false,
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '电子签合同模板',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    searchQuery();
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
   
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }
   
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        },
      },
    ];
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }

  /**
   * 同步线上模板
   */
  async function handleSyncOnlineTemplate() {
    try {
      createMessage.loading('正在同步线上模板...', 0);
      const result = await syncOnlineTemplates(handleSuccess);
      createMessage.destroy();

      if (result.success) {
        createMessage.success(result.message);
      } else {
        createMessage.error(result.message);
      }
    } catch (error) {
      createMessage.destroy();
      createMessage.error('同步模板失败');
      console.error('同步模板失败:', error);
    }
  }
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    padding: 0;
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust{
      min-width: 100px !important;
    }
    .query-group-split-cust{
      width: 30px;
      display: inline-block;
      text-align: center
    }
  }
</style>

<style lang="less">
  // 合同名字列自动换行样式
  :deep(.ant-table-tbody .ant-table-cell) {
    white-space: normal !important;
    word-break: break-word !important;
    line-height: 1.4 !important;
  }
</style>